home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / netzwerk / parnet / sources / task.c < prev    next >
C/C++ Source or Header  |  1996-02-26  |  3KB  |  134 lines

  1.  
  2. /*
  3.  *  TASK.C
  4.  *
  5.  *  -Accept packets to send to network
  6.  *  -Receive data from network
  7.  *
  8.  *        ParWrite(destaddr, buf, bytes)
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. /* par.asm */
  14. extern int ParDataReady(void);
  15. extern int ParReadV(void *, long, ...);
  16. extern int ParWriteV(long dest, void *, long, ...);
  17.  
  18. typedef struct {
  19.     uword   Port;   /*    destination port    */
  20.     uword   ChkSum; /*    data checksum        */
  21.     ulong   Bytes;  /*    # of bytes        */
  22. } Header;
  23.  
  24. char    DataBuf[MAXPKTSIZE];
  25. Header    Hdr;
  26. long    TLock[2] = { 0, 0 };
  27. static    short Cnt = 0;
  28.  
  29. /* prototyping */
  30. void CParNetTask(void);
  31. int OutputPacket(Packet *);
  32.  
  33. void
  34. CParNetTask(void)
  35. {
  36.     long mask;
  37.     Unit *unit;
  38.     Packet *packet;
  39.     long n;
  40.  
  41.     for (;;) {
  42.     mask = Wait(SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F);
  43.     LockAddr(TLock);
  44.     if (mask & SIGBREAKF_CTRL_F) {      /*  packet pending  */
  45. data:
  46.         if (ParDataReady() > 0) {
  47.         n = ParReadV(&Hdr, sizeof(Hdr), DataBuf, MAXPKTSIZE, NULL, NULL);
  48.  
  49. #ifdef DEBUG
  50.         sprintf(StickyPort->DebugBuf, "%03d READ %6ld bytes port %d", Cnt++,n, Hdr.Port);
  51. #endif
  52.  
  53.         if (n >= sizeof(Hdr) && n >= sizeof(Hdr) + Hdr.Bytes) {
  54.             if (unit = FindUnitForPort(Hdr.Port)) {
  55.             packet = AllocParPacket(NULL, unit, DataBuf, Hdr.Bytes, NULL, 0);
  56.             LockAddr(unit->UnitLock);
  57.             (*packet->io_Unit->DataFunc)('r', packet, n - sizeof(Hdr));
  58.             UnlockAddr(unit->UnitLock);
  59.             }
  60.         }
  61.         }
  62.     }
  63.     if (mask & SIGBREAKF_CTRL_E) {      /*  port            */
  64.         while (packet = (Packet *)GetMsg(&DevBase->Port)) {
  65.         n = OutputPacket(packet);
  66.         if (n == -2) {
  67.             Forbid();
  68.             AddHead(&DevBase->Port.mp_MsgList, (struct Node *)packet);
  69.             Permit();
  70.             goto data;
  71.         }
  72.         }
  73.     }
  74.     UnlockAddr(TLock);
  75.     }
  76. }
  77.  
  78. int
  79. OutputPacket(packet)
  80. Packet *packet;
  81. {
  82.     long n;
  83.     Unit *unit = packet->io_Unit;
  84.  
  85.     Hdr.Port  = packet->DestPort;
  86.     Hdr.ChkSum= 0;
  87.     Hdr.Bytes = packet->DLen1 + packet->DLen2;
  88.  
  89.     n = ParWriteV(packet->DestAddr, &Hdr, sizeof(Hdr), packet->Data1, packet->DLen1, packet->Data2, packet->DLen2, NULL, NULL);
  90.  
  91. #ifdef DEBUG
  92.     sprintf(StickyPort->DebugBuf, "%03d WRITE %6ld to %d port %d", Cnt++,
  93.         n,packet->DestAddr,packet->DestPort);
  94. #endif
  95.  
  96.     if (n == -2)                /*  can't write, pending rcv    */
  97.     return(n);
  98.     LockAddr(unit->UnitLock);
  99.     if (n == sizeof(Hdr) + packet->DLen1 + packet->DLen2)
  100.     (*unit->DataFunc)('w', packet, n - sizeof(Hdr));
  101.     else
  102.     (*unit->DataFunc)('W', packet, n - sizeof(Hdr));
  103.     UnlockAddr(unit->UnitLock);
  104.     return(n);
  105. }
  106.  
  107.  
  108. /*
  109.  *  Queue packet for write.  If QUICKIO is requested and the packet can be
  110.  *  sent manually it is, else it is queued to the task.
  111.  */
  112.  
  113. void
  114. QueuePacketForWrite(packet)
  115. Packet *packet;
  116. {
  117.     Iob *iob = packet->iob;
  118.  
  119.     if ((iob->io_Flags & IOF_QUICK) && TryLockAddr(TLock) > 0) {
  120.     if (OutputPacket(packet) != -2) {
  121.         UnlockAddr(TLock);
  122.         return;
  123.     }
  124.     /*
  125.      *  Can't write packet, rcv packet pending.
  126.      */
  127.     UnlockAddr(TLock);
  128.     }
  129.     iob->io_Flags &= ~IOF_QUICK;
  130.     iob->io_Flags |= IOF_QUEUED;
  131.     PutMsg(&DevBase->Port, &packet->Msg);
  132. }
  133.  
  134.